# -*- coding: utf-8 -*-
from flask import json
import redis
from scrapy_redis.spiders import RedisSpider
from scrapy import Request, FormRequest
from spider.ops import Ops
from facility import logger


class LagouSpider(RedisSpider):
    name = 'lagou'
    redis_key = 'lagou:start_urls'
    v8host = 'http://localhost:5000/'

    def parse(self, response):
        yield Request(url=self.v8host + 'lagou', dont_filter=True, callback=self.parse_item)

    def parse_item(self, response):
        q = Ops(response)
        enum = q.get_num('//*[@id="s_position_list"]/ul/li')
        if enum == 0:
            logger(self.name, '死亡')
            logger(self.name, '死亡')
            logger(self.name, '死亡')

        for i in range(enum):
            name = q.get_str('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[1]/a/h3'.format(i + 1))
            link = q.get_attr('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[1]/a'.format(i + 1), 'href')
            amount = q.get_str('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[2]/div/span'.format(i + 1))
            company = q.get_str('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[2]/div[1]/a'.format(i + 1))

            yield {
                "id": name,
                "link": link,
                "amount": amount,
                "company": company,
            }
